From 48232d3d931c95953ce2ddfe7da7bb164aef6a73 Mon Sep 17 00:00:00 2001
From: Felix Fietkau <nbd@nbd.name>
Date: Fri, 7 Jul 2017 17:03:16 +0200
Subject: fix portability of some includes files in tools/ used on the host

Signed-off-by: Felix Fietkau <nbd@nbd.name>
---
 tools/include/tools/be_byteshift.h |  4 ++++
 tools/include/tools/le_byteshift.h |  4 ++++
 tools/include/tools/linux_types.h  | 22 ++++++++++++++++++++++
 3 files changed, 30 insertions(+)
 create mode 100644 tools/include/tools/linux_types.h

--- a/tools/include/tools/be_byteshift.h
+++ b/tools/include/tools/be_byteshift.h
@@ -2,6 +2,10 @@
 #ifndef _TOOLS_BE_BYTESHIFT_H
 #define _TOOLS_BE_BYTESHIFT_H
 
+#ifndef __linux__
+#include "linux_types.h"
+#endif
+
 #include <stdint.h>
 
 static inline uint16_t __get_unaligned_be16(const uint8_t *p)
--- a/tools/include/tools/le_byteshift.h
+++ b/tools/include/tools/le_byteshift.h
@@ -2,6 +2,10 @@
 #ifndef _TOOLS_LE_BYTESHIFT_H
 #define _TOOLS_LE_BYTESHIFT_H
 
+#ifndef __linux__
+#include "linux_types.h"
+#endif
+
 #include <stdint.h>
 
 static inline uint16_t __get_unaligned_le16(const uint8_t *p)
--- /dev/null
+++ b/tools/include/tools/linux_types.h
@@ -0,0 +1,18 @@
+#ifndef __LINUX_TYPES_H
+#define __LINUX_TYPES_H
+
+#include <stdint.h>
+
+typedef int8_t __s8;
+typedef uint8_t __u8;
+
+typedef int16_t __s16;
+typedef uint16_t __u16;
+
+typedef int32_t __s32;
+typedef uint32_t __u32;
+
+typedef int64_t __s64;
+typedef uint64_t __u64;
+
+#endif
--- a/tools/include/linux/types.h
+++ b/tools/include/linux/types.h
@@ -10,8 +10,12 @@
 #define __SANE_USERSPACE_TYPES__	/* For PPC64, to get LL64 types */
 #endif
 
+#ifndef __linux__
+#include <tools/linux_types.h>
+#else
 #include <asm/types.h>
 #include <asm/posix_types.h>
+#endif
 
 struct page;
 struct kmem_cache;
@@ -51,7 +55,9 @@ typedef __s8  s8;
 #define __force
 #define __user
 #define __must_check
+#ifndef __cold
 #define __cold
+#endif
 
 typedef __u16 __bitwise __le16;
 typedef __u16 __bitwise __be16;
--- a/tools/perf/pmu-events/jevents.py
+++ b/tools/perf/pmu-events/jevents.py
@@ -684,6 +684,7 @@ def main() -> None:
 #include "util/header.h"
 #include "util/pmu.h"
 #include <string.h>
+#include <strings.h>
 #include <stddef.h>
 
 struct compact_pmu_event {
--- a/tools/arch/x86/include/asm/insn.h
+++ b/tools/arch/x86/include/asm/insn.h
@@ -7,8 +7,8 @@
  * Copyright (C) IBM Corporation, 2009
  */
 
-#include <asm/byteorder.h>
 /* insn_attr_t is defined in inat.h */
+#include <linux/kernel.h>
 #include "inat.h" /* __ignore_sync_check__ */
 
 #if defined(__BYTE_ORDER) ? __BYTE_ORDER == __LITTLE_ENDIAN : defined(__LITTLE_ENDIAN)
--- a/tools/arch/x86/include/asm/orc_types.h
+++ b/tools/arch/x86/include/asm/orc_types.h
@@ -40,7 +40,6 @@
 #define ORC_REG_MAX			15
 
 #ifndef __ASSEMBLY__
-#include <asm/byteorder.h>
 
 /*
  * This struct is more or less a vastly simplified version of the DWARF Call
@@ -53,12 +52,12 @@
 struct orc_entry {
 	s16		sp_offset;
 	s16		bp_offset;
-#if defined(__LITTLE_ENDIAN_BITFIELD)
+#if __BYTE_ORDER == __LITTLE_ENDIAN
 	unsigned	sp_reg:4;
 	unsigned	bp_reg:4;
 	unsigned	type:2;
 	unsigned	end:1;
-#elif defined(__BIG_ENDIAN_BITFIELD)
+#elif __BYTE_ORDER == __BIG_ENDIAN
 	unsigned	bp_reg:4;
 	unsigned	sp_reg:4;
 	unsigned	unused:5;
--- a/tools/arch/x86/lib/insn.c
+++ b/tools/arch/x86/lib/insn.c
@@ -15,7 +15,11 @@
 #include "../include/asm/insn.h" /* __ignore_sync_check__ */
 #include "../include/asm-generic/unaligned.h" /* __ignore_sync_check__ */
 
+#ifdef __KERNEL__
 #include <linux/errno.h>
+#else
+#include <errno.h>
+#endif
 #include <linux/kconfig.h>
 
 #include "../include/asm/emulate_prefix.h" /* __ignore_sync_check__ */
--- a/tools/include/asm-generic/bitops/fls.h
+++ b/tools/include/asm-generic/bitops/fls.h
@@ -2,6 +2,8 @@
 #ifndef _ASM_GENERIC_BITOPS_FLS_H_
 #define _ASM_GENERIC_BITOPS_FLS_H_
 
+#include <string.h>
+
 /**
  * fls - find last (most-significant) bit set
  * @x: the word to search
@@ -10,7 +12,7 @@
  * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32.
  */
 
-static __always_inline int fls(unsigned int x)
+static __always_inline int __generic_fls(unsigned int x)
 {
 	int r = 32;
 
@@ -38,5 +40,6 @@ static __always_inline int fls(unsigned
 	}
 	return r;
 }
+#define fls __generic_fls
 
 #endif /* _ASM_GENERIC_BITOPS_FLS_H_ */
--- a/tools/include/asm-generic/bitsperlong.h
+++ b/tools/include/asm-generic/bitsperlong.h
@@ -4,11 +4,13 @@
 
 #include <uapi/asm-generic/bitsperlong.h>
 
+#ifndef BITS_PER_LONG
 #ifdef __SIZEOF_LONG__
 #define BITS_PER_LONG (__CHAR_BIT__ * __SIZEOF_LONG__)
 #else
 #define BITS_PER_LONG __WORDSIZE
 #endif
+#endif
 
 #if BITS_PER_LONG != __BITS_PER_LONG
 #error Inconsistent word size. Check asm/bitsperlong.h
--- a/tools/include/linux/rbtree.h
+++ b/tools/include/linux/rbtree.h
@@ -18,7 +18,6 @@
 #define __TOOLS_LINUX_PERF_RBTREE_H
 
 #include <linux/kernel.h>
-#include <linux/stddef.h>
 
 struct rb_node {
 	unsigned long  __rb_parent_color;
--- a/tools/objtool/Makefile
+++ b/tools/objtool/Makefile
@@ -4,7 +4,7 @@ include ../scripts/Makefile.arch
 
 # always use the host compiler
 AR	 = $(HOSTAR)
-CC	 = $(HOSTCC)
+CC	 = $(HOSTCC) $(HOST_EXTRACFLAGS)
 LD	 = $(HOSTLD)
 
 ifeq ($(srctree),)
@@ -43,6 +43,7 @@ BUILD_ORC := n
 
 ifeq ($(SRCARCH),x86)
 	BUILD_ORC := y
+	CFLAGS += -DBUILD_ORC
 endif
 
 export BUILD_ORC
--- a/tools/objtool/check.c
+++ b/tools/objtool/check.c
@@ -1164,11 +1164,12 @@ static int add_ignore_alternatives(struc
 	return 0;
 }
 
+#ifndef BUILD_ORC
 /*
  * Symbols that replace INSN_CALL_DYNAMIC, every (tail) call to such a symbol
  * will be added to the .retpoline_sites section.
  */
-__weak bool arch_is_retpoline(struct symbol *sym)
+bool arch_is_retpoline(struct symbol *sym)
 {
 	return false;
 }
@@ -1177,7 +1178,7 @@ __weak bool arch_is_retpoline(struct sym
  * Symbols that replace INSN_RETURN, every (tail) call to such a symbol
  * will be added to the .return_sites section.
  */
-__weak bool arch_is_rethunk(struct symbol *sym)
+bool arch_is_rethunk(struct symbol *sym)
 {
 	return false;
 }
@@ -1186,10 +1187,11 @@ __weak bool arch_is_rethunk(struct symbo
  * Symbols that are embedded inside other instructions, because sometimes crazy
  * code exists. These are mostly ignored for validation purposes.
  */
-__weak bool arch_is_embedded_insn(struct symbol *sym)
+bool arch_is_embedded_insn(struct symbol *sym)
 {
 	return false;
 }
+#endif
 
 #define NEGATIVE_RELOC	((void *)-1L)
 
--- a/tools/objtool/include/objtool/objtool.h
+++ b/tools/objtool/include/objtool/objtool.h
@@ -12,7 +12,9 @@
 
 #include <objtool/elf.h>
 
+#ifndef __weak
 #define __weak __attribute__((weak))
+#endif
 
 struct pv_state {
 	bool clean;
--- a/tools/objtool/orc_dump.c
+++ b/tools/objtool/orc_dump.c
@@ -5,10 +5,10 @@
 
 #include <unistd.h>
 #include <linux/objtool.h>
-#include <asm/orc_types.h>
 #include <objtool/objtool.h>
 #include <objtool/warn.h>
 #include <objtool/endianness.h>
+#include <asm/orc_types.h>
 
 static const char *reg_name(unsigned int reg)
 {
--- a/tools/objtool/orc_gen.c
+++ b/tools/objtool/orc_gen.c
@@ -7,11 +7,11 @@
 #include <string.h>
 
 #include <linux/objtool.h>
-#include <asm/orc_types.h>
 
 #include <objtool/check.h>
 #include <objtool/warn.h>
 #include <objtool/endianness.h>
+#include <asm/orc_types.h>
 
 static int init_orc_entry(struct orc_entry *orc, struct cfi_state *cfi,
 			  struct instruction *insn)
--- a/tools/objtool/special.c
+++ b/tools/objtool/special.c
@@ -54,9 +54,11 @@ struct special_entry entries[] = {
 	{},
 };
 
-void __weak arch_handle_alternative(unsigned short feature, struct special_alt *alt)
+#ifndef BUILD_ORC
+void arch_handle_alternative(unsigned short feature, struct special_alt *alt)
 {
 }
+#endif
 
 static void reloc_to_sec_off(struct reloc *reloc, struct section **sec,
 			     unsigned long *off)
--- a/tools/objtool/weak.c
+++ b/tools/objtool/weak.c
@@ -15,12 +15,14 @@
 	return ENOSYS;							\
 })
 
-int __weak orc_dump(const char *_objname)
+#ifndef BUILD_ORC
+int orc_dump(const char *_objname)
 {
 	UNSUPPORTED("ORC");
 }
 
-int __weak orc_create(struct objtool_file *file)
+int orc_create(struct objtool_file *file)
 {
 	UNSUPPORTED("ORC");
 }
+#endif
--- a/tools/scripts/Makefile.include
+++ b/tools/scripts/Makefile.include
@@ -92,7 +92,7 @@ LLVM_OBJCOPY	?= llvm-objcopy
 LLVM_STRIP	?= llvm-strip
 
 ifeq ($(CC_NO_CLANG), 1)
-EXTRA_WARNINGS += -Wstrict-aliasing=3
+# EXTRA_WARNINGS += -Wstrict-aliasing=3
 
 else ifneq ($(CROSS_COMPILE),)
 # Allow userspace to override CLANG_CROSS_FLAGS to specify their own
